<?php
namespace Api\Controller;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

use Gy_Library\CusSession;
use Gy_Library\RestController;

/**
 * Description of LoginController
 *
 * @author hardy
 */
trait DataMachiningController{
    /**
     * 插入数据库
     * 支持事务
     * @param $model
     * @param $data
     * @return mixed
     */
    public function createInsertDB($model, $data){
        $r = $model->createAdd($data);
        if($r === FALSE){
            $this->setFailMessage($model->getError());
        }
        return $r;
    }

    /**
     * 更新数据
     * 不支持事务
     * @param $model
     * @param $data
     */
    public function updateTableData($model, $data){
        if($model->createSave($data) === false){
            $this->setFailMessage($model->getError());
        }else{
            $oneData = $model->getOne($data['id']);
            $this->setSuccessMessageOneData($oneData);
        }
    }
    /**
     * 方法使用的 用来以分页形式返回数据列表
     * @param $model 模型
     * @param $map 条件
     * @param string $order 排序
     * @param string $function_name
     * @param string $function_count
     */
    public function getPageData($model, $map, $order = 'id desc', $function_name = 'getListForPage',$function_count='getListForCount'){
        $count = $model->{$function_count}($map);
        $per_page = C('ADMIN_PER_PAGE_NUM', null, false);
        if($per_page === false){
            $page = new \Gy_Library\GyPage($count);
        }
        else{
            $page = new \Gy_Library\GyPage($count, $per_page);
        }
        $data_list = $model->{$function_name}($map, $page->nowPage, $page->listRows, $order);
        $this->setSuccessMessage($data_list);
    }
    /**
     * 成功
     * @param $data
     * @param string $message
     */
    public function setSuccessMessage($data=[], $message='成功提交'){
        $jsonData['count'] = count($data);
        if(empty($data)){
            $jsonData['list'] = [];
        }else{
            $jsonData['list'] = $data;
        }
        $this->response($message, 1, $jsonData);
    }

    /**
     * 成功，返回一条信息记录
     * @param array $data
     * @param string $message
     */
    public function setSuccessMessageOneData($data=[], $message='成功提交'){
        if(empty($data)){
            $this->response($message, 1, []);
        }else{
            $this->response($message, 1, $data);
        }
    }

    /**
     * 失败
     * @param string $message
     * @param array $data
     * @param int $code
     */
    public function setFailMessage($message='访问出错了，请重新访问',$data = [],$code=200){
        if(empty($data)){
            $this->response($message,0,[],$code);
        }else{
            $this->response($message,0,$data,$code);
        }

    }

    /**
     * 获得session ID -》 学生 id
     * @return mixed
     */
    public function getSessionId(){
        $user = CusSession::get(C('AUTH_ID'));
        return $user['id'];
    }

    /**
     * 删除一条数据
     * @param $model
     * @param $map
     * @param string $delete_function
     */
    public function deleteModelData($model, $map, $delete_function='deleteData'){
        $map['id'] = I('get.id');
        if(empty($map['id'])){
            $this->setFailMessage('错误的访问');
        }
        $r = $model->{$delete_function}($map);
        if($r === FALSE){
            $this->setFailMessage($model->_error);
        }else{
            $this->setSuccessMessageOneData();
        }
    }

    /**
     * 更改某个表的数据数据
     * @param $ent
     * @param string $mode
     * @throws \ReflectionException
     */
    public function changeDataSave($ent, $mode='put.'){
        $data = I($mode);
        $className = new \ReflectionClass($this);
        //检测是否需要过滤key
        $unwantedKey = [];
        if(method_exists($className->name, 'getUnwantedKey')){
            $unwantedKey = $this->getUnwantedKey();
        }
        foreach ($data as $key => $value){
            if(in_array($key, $unwantedKey, true)){
                continue ;
            }else{
                $ent[$key] = $value;
            }
        }
        $model = D(CONTROLLER_NAME);
        if($model->createSave($ent) === false){
            $this->setFailMessage($model->getError());
        }else{
            $oneData = $model->getOne($ent['id']);
            $this->setSuccessMessageOneData($oneData);
        }
    }
}
